package pe.com.modulo.gps;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ws.gps.coordenadas.WSInfoGPSProxy;

import com.google.gson.Gson;

@SuppressWarnings("serial")
@WebServlet("/buscarcoordenadas")
public class gpsCoordenadasServlet extends HttpServlet{

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
//		String sPlaca = request.getParameter("strPlaca");
//
//		Gson gson = new Gson();
//		String json = null;
//		
//		String data = obtenerCoordenadas(sPlaca);
//		String datosArray[] = data.split("&");
//		
//		coordenadasBean coordenadas = new coordenadasBean();
//		coordenadas.setPlaca(datosArray[0].toString());
//		coordenadas.setLongitud(datosArray[1].toString());
//		coordenadas.setLatitud(datosArray[2].toString());
//		coordenadas.setIdgps(datosArray[3].toString());
//		
//		json = gson.toJson(coordenadas);
//
//		request.setCharacterEncoding("utf-8");
//		response.setContentType("application/json;charset=utf-8");
//		PrintWriter out = response.getWriter();		
//		out.print(json);
//		out.flush();
	}	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println("Post gpsCoordenadasServlet");
		
		String sPlaca = request.getParameter("strPlaca");
		
		try {

			sPlaca = request.getParameter("strPlaca")==null?"":request.getParameter("strPlaca").trim();

			String urlType = "http://localhost:8080/WSGPG/services/WSInfoGPS?wsdl";//common.getURL_AMC(map);

			Gson gson = new Gson();
			String cadena = gson.toJson(urlType);
			
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(cadena);
			out.flush();

		} catch (Exception e) {

			System.out.println("Error: "+e.getMessage());

		}

	}

	
	public String obtenerCoordenadas(String sPlaca){
		
		WSInfoGPSProxy proxy = new WSInfoGPSProxy();
		
		String data = null;
		
		try {
			
			data = proxy.obtenerCoordenadasGPS(sPlaca);
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return data;
	}
}
